const formatTime = date => {
    const year = date.getFullYear();
    const month = date.getMonth() + 1;
    const day = date.getDate();
    const hour = date.getHours();
    const minute = date.getMinutes();
    const second = date.getSeconds();
    return [year, month, day].map(formatNumber).join("/") + " " + [hour, minute, second].map(formatNumber).join(":");
};


const formatNumber = n => {
    n = n.toString();
    return n[1] ? n : "0" + n;
};


// 显示繁忙提示
var showBusy = function (text) {
    return wx.showToast({
        title: text,
        icon: "loading",
        duration: 10000
    });
};

// 显示成功提示
var showSuccess = text => wx.showToast({
    title: text,
    icon: "success"
});

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: JSON.stringify(content),
        showCancel: false
    })
};

const STATION_LIST_KEY = "STATION_LIST_KEY";
const FACTORY_LIST_KEY = "FACTORY_LIST_KEY";
const PARAMETER_LIST_KEY = "PARAMETER_LIST_KEY";
const ADMINISTRATIVE_DIVISION_LIST_KEY = "ADMINISTRATIVE_DIVISION_LIST_KEY";
let instance = null;
export default class GlobalData {
    constructor() {
        if (!instance) instance = this;
        return instance;
    }

    setStationList(list) {
        wx.setStorage({
            key: STATION_LIST_KEY,
            data: list,
        });
    }

    setFactoryList(list) {
        wx.setStorage({
            key: FACTORY_LIST_KEY,
            data: list,
        });
    }

    setParameterList(list) {
        wx.setStorage({
            key: PARAMETER_LIST_KEY,
            data: list,
        });
    }

    setAdministrativeDivisionList(list) {
        wx.setStorage({
            key: ADMINISTRATIVE_DIVISION_LIST_KEY,
            data: list,
        });
    }

    getStationList() {
        return wx.getStorageSync(STATION_LIST_KEY);
    }

    getFactoryList() {
        return wx.getStorageSync(FACTORY_LIST_KEY);
    }

    getParameterList() {
        return wx.getStorageSync(PARAMETER_LIST_KEY);
    }

    getAdministrativeDivisionList() {
        return wx.getStorageSync(ADMINISTRATIVE_DIVISION_LIST_KEY);
    }

    getStationMap() {
        let stationList = GlobalData.getStationList();
        let stationMap = {};
        for (let i = 0; i < stationList.length; i++) {
            let bean = stationList[i];
            stationMap[bean.id] = bean;
        }
        return stationMap;
    }
}
module.exports = {formatTime, showBusy, showSuccess, showModel};
